package com.wuuxiang.polestar.admin.web;

import com.wuuxiang.polestar.admin.dto.BaseReturnDTO;
import com.wuuxiang.polestar.admin.dto.PropertyDto;
import com.wuuxiang.polestar.admin.dto.PropertyRetDto;
import com.wuuxiang.polestar.admin.service.property.IPropertyService;
import com.wuuxiang.polestar.admin.service.team.ITeamService;
import com.wuuxiang.polestar.admin.utils.OvalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * 项目名称：polestar        <br>
 * 类描述：                         <br>
 * 创建人：夏鸿鹏                    <br>
 * 创建时间：2017-11-28 14:44        <br>
 * 修改备注：                        <br>
 */

@RestController
public class PropertyController {

    @Autowired
    private IPropertyService propertyService;
    @Autowired
    private ITeamService teamService;

    @RequestMapping("/polestar/createProperty")
    @ResponseBody
    public BaseReturnDTO createProperty(@RequestBody PropertyDto propertyDto) {
        BaseReturnDTO baseReturnDTO = new BaseReturnDTO();
        if (OvalUtils.validate(baseReturnDTO, propertyDto)) {
            return baseReturnDTO;
        }
        String teamName = propertyDto.getTeamName().trim();
        String name = propertyDto.getPropertyName().trim();
        if (propertyService.checkExists(teamName, name)) {
            baseReturnDTO.setReturnCode(-1);
            baseReturnDTO.setErrorCode("500");
            baseReturnDTO.setErrorText("配置文件已经存在!");
        } else {
            propertyService.createProperty(teamName, name, "");
        }
        return baseReturnDTO;
    }

    @RequestMapping("/polestar/removeProperty")
    @ResponseBody
    public BaseReturnDTO removeProperty(@RequestBody PropertyDto propertyDto) {
        BaseReturnDTO baseReturnDTO = new BaseReturnDTO();
        if (OvalUtils.validate(baseReturnDTO, propertyDto)) {
            return baseReturnDTO;
        }
        String teamName = propertyDto.getTeamName().trim();
        String name = propertyDto.getPropertyName().trim();
        if (propertyService.checkExists(teamName, name)) {
            propertyService.removeProperty(teamName, name);
        }
        return baseReturnDTO;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/polestar/getPropertyLst")
    @ResponseBody
    public PropertyRetDto getPropertyLst() {
        PropertyRetDto propertyRetDto = new PropertyRetDto();
        List<String> lst = teamService.getTeamLst();
        if ((null != lst) && (lst.size() > 0)) {
            HashMap<String, List<String>> propertyRLst = new HashMap<String, List<String>>();
            for (String teamName : lst) {
            	List<String> prolst = propertyService.getPropertyLst(teamName);
            	if (null != prolst) {
            		Collections.sort(prolst);
            	}
                propertyRLst.put(teamName, prolst);
            }
            propertyRetDto.setPropertyRLst(propertyRLst);
        }
        return propertyRetDto;
    }

}
